package com.fluo;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class Login extends Activity implements SyncElement{
		
	private EditText username;
	private EditText password;
	
    static List<String> desktopList = new ArrayList<String>();
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
		username = (EditText) findViewById(R.id.username_edit);
		password = (EditText) findViewById(R.id.password_edit);
		
		desktopList.clear();
		
		////////////////////////////
		// Register Button
		////////////////////////////
        final ImageButton registerbutton = (ImageButton) findViewById(R.id.register_button);
        registerbutton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

        		Socket socket;
        		ObjectInputStream input;
        		ObjectOutputStream output;

            	// Connection Setup
            	try {
        		
            	socket = new Socket(SERVER_NAME, SERVER_PORT);

            	output = new ObjectOutputStream(socket.getOutputStream());
            	input = new ObjectInputStream(socket.getInputStream());
            	
        		FluoMessage msg;
            	
    			String un = username.getText().toString();
    			String pw = password.getText().toString();
    			
            	// Send Main Server username and password
        		msg = new FluoMessage(un, pw, "register");
				output.writeObject(msg);

        		// Wait for acceptance of password
        		msg = (FluoMessage) input.readObject();
        		if(msg.getSingleString().equalsIgnoreCase("YES"))
        		{	
            		desktopList.add("Add yourself to a desktop server.");  		
            		//Next =>
            		startActivity(new Intent(Login.this, ServerConnect.class));
        		}
            	
        		else
        		{
    				username.setText("");
    				password.setText("");
                    Toast.makeText(Login.this, "Username is taken", Toast.LENGTH_SHORT).show();            	    
        		}
        		
        		} catch (UnknownHostException e) {
        			// TODO Auto-generated catch block
        			e.printStackTrace();
        		} catch (IOException e) {
        			// TODO Auto-generated catch block
        			e.printStackTrace();
        		} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        		        		
        	    /////////////////////////////////////
                //TESTING
        		desktopList.add("Add yourself to a desktop server.");  		
            	//Next =>
        	    startActivity(new Intent(Login.this, ServerConnect.class));
        	    /////////////////////////////////////       		
            }
        }); 
		
		////////////////////////////
		// Next Button
		////////////////////////////
        final ImageButton nextbutton = (ImageButton) findViewById(R.id.next_button);
        nextbutton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

        		Socket socket;
        		ObjectInputStream input;
        		ObjectOutputStream output;

            	// Connection Setup
            	try {
        		
            	socket = new Socket(SERVER_NAME, SERVER_PORT);

            	output = new ObjectOutputStream(socket.getOutputStream());
            	input = new ObjectInputStream(socket.getInputStream());
            	
        		FluoMessage msg;
            	
    			String un = username.getText().toString();
    			String pw = password.getText().toString();
    			
            	// Send Main Server username and password
        		msg = new FluoMessage(un, pw);
				output.writeObject(msg);

        		// Wait for acceptance of password
        		msg = (FluoMessage) input.readObject();
        		if(msg.getSingleString().equalsIgnoreCase("YES"))
        		{	
            		msg = (FluoMessage) input.readObject();
            		desktopList = msg.getDSList();
            		
            	//Next =>
        	    startActivity(new Intent(Login.this, ServerConnect.class));
        		}
            	
        		else
        		{
    				username.setText("");
    				password.setText("");
                    Toast.makeText(Login.this, "Username or Password is incorrect!", Toast.LENGTH_SHORT).show();            	    
        		}
        		
        		} catch (UnknownHostException e) {
        			// TODO Auto-generated catch block
        			e.printStackTrace();
        		} catch (IOException e) {
        			// TODO Auto-generated catch block
        			e.printStackTrace();
        		} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        		        		
        	    /////////////////////////////////////
                //TESTING
                //Next =>              
                desktopList.add("127.0.0.1:1234");
        	    startActivity(new Intent(Login.this, ServerConnect.class));
        	    /////////////////////////////////////       		
            }
        }); 
    }
}